<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.roleName" size="default" placeholder="请输入角色名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增角色
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" align="center" width="60" />
				<el-table-column prop="roleName" label="角色名称" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="desc" label="角色描述" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createAt" label="创建时间" align="center" show-overflow-tooltip>
					<template #default="scope">
						{{ formatDate(new Date(scope.row.createAt),'YYYY-mm-dd HH:MM:SS') }}
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="100">
					<!-- <template #default="scope">
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" v-auth="'role:edit'" @click="onOpenEditRole('edit', scope.row)">修改</el-button>
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" v-auth="'role:del'" @click="onRowDel(scope.row)">删除</el-button>
					</template> -->
					<template #default="scope">
						<el-button size="small" text type="primary"  @click="onOpenEditRole('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary"  @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页组件 -->
			<Pagination 
				class="mt15"
				:total="state.tableData.total"
				:page="state.tableData.param.pageNum"
				:limit="state.tableData.param.pageSize"
				@pagination="pageChange"
			/>
		</div>
		<AddRole ref="dialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { formatDate } from '/@/utils/formatTime';
// 后台接口
import { systemApi } from '/@/api/system/index';

// 引入组件
const AddRole = defineAsyncComponent(() => import('./modal/addRole.vue'));

// 定义变量内容
const dialogRef = ref();
const state = reactive<SysRoleState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			roleName: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
  let res: any = await systemApi().getRoleList(state.tableData.param)
	state.tableData.data = res.datas.list;
	state.tableData.total = res.datas.count;
  state.tableData.loading = false;
};
// 打开新增角色弹窗
const onOpenAddRole = (type: string) => {
	dialogRef.value.openDialog(type);
};
// 打开修改角色弹窗
const onOpenEditRole = (type: string, row: Object) => {
	dialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowRoleType) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res:any = await systemApi().delRole({id: row.id});
      if(res.success){
        getTableData();
        ElMessage.success("删除成功");
      }else{
        ElMessage.error("删除失败");
      }
		})
		.catch(() => {});
};
// 分页组件改变
const pageChange = (pageObj: {pageNum:number,pageSize:number}) => {
	state.tableData.param.pageNum = pageObj.pageNum;
	state.tableData.param.pageSize = pageObj.pageSize;
	getTableData();
}
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
